package com.louis.kitty.zuul;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

@Component
public class MyFilter extends ZuulFilter {
    private List<String> trustHosts = new ArrayList<String>();// ip白名单
    private List<String> unTrustHosts = new ArrayList<String>(); // ip黑名单

    private MyFilter() {
        this.trustHosts.add("10.136.41.45");
//        this.unTrustHosts.add("10.136.41.46");
    }

    private static Logger log = LoggerFactory.getLogger(MyFilter.class);

    @Override
    public String filterType() {
        return "pre"; // 定义filter的类型，有pre、route、post、error四种
    }

    @Override
    public int filterOrder() {
        return 0; // 定义filter的顺序，数字越小表示顺序越高，越先执行
    }

    @Override
    public boolean shouldFilter() {
        return true; // 表示是否需要执行该filter，true表示执行，false表示不执行
    }

    @Override
    public Object run() throws ZuulException {
        // filter需要执行的具体操作
        try {
            RequestContext ctx = RequestContext.getCurrentContext();
            HttpServletRequest request = ctx.getRequest();
            String token = request.getParameter("acktoken");
//        log.warn("远程访问地址:"+request.getRemoteAddr());
//        log.warn("远程主机:"+request.getRemoteHost());
//        log.warn("Token:"+request.getHeader("token"));
//
//        if(request.getCookies()!=null&&request.getCookies().length>2){
//            log.warn("cookies获得用户:"+request.getCookies()[2].getValue());
//        }
//        log.warn("sessionId:"+request.getRequestedSessionId());
//        log.warn("引用:"+request.getRequestedSessionId());
//        log.warn("从什么页面跳转："+request.getHeader("referer"));
//        log.warn("请求url:"+request.getRequestURI());
//        log.warn("请求方法类型："+request.getMethod());
//        log.warn("请求方法参数："+request.getParameterMap());

            if (trustHosts.contains(request.getRemoteAddr())) {// 白名单直接访问
//            log.info("通过白名单访问ok");
                return null;
            }

            if (request.getHeader("referer") != null) {// 通过网页访问放行
//            log.info("通过系统网页操作访问ok");
                return null;
            }

//            if (unTrustHosts.contains(request.getRemoteAddr())) {// 黑名单禁止
////            log.warn("黑名单访问禁止");
//                ctx.setSendZuulResponse(false);
//                ctx.setResponseStatusCode(401);
//                try {
//                    ctx.getResponse().getWriter().write("黑名单访问禁止");
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                return null;
//            }

            if (token == null) {
                log.warn("没有访问权限");
                ctx.setSendZuulResponse(false);
                ctx.setResponseStatusCode(401);
                try {
                    ctx.getResponse().getWriter().write("没有访问权限");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            } else if (!token.equals("cmmes123!@")) {// 不受信任主机需要加token验证
                ctx.setSendZuulResponse(false);
                ctx.setResponseStatusCode(401);
                try {
                    ctx.getResponse().getWriter().write("token不正确");
                } catch (IOException e) {
                    e.printStackTrace();
                }
//            log.warn("token不正确");

                return null;
            }
        } catch (Exception e) {
            return null;
        }

//        log.info("ok");
        return null;
    }
}